home *** CD-ROM | disk | FTP | other *** search
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
-
- #include "htsglobal.h"
- #include "htsbase.h"
-
- #include "HTTrackInterface.h"
-
- // Lecture d'une ligne (peut Ítre unicode ‡ priori)
- int linput(FILE* fp,char* s,int max) {
- int c;
- int j=0;
- do {
- c=fgetc(fp);
- if (c!=EOF) {
- switch(c) {
- case 13: break; // sauter CR
- case 10: c=-1; break;
- case 9: case 12: break; // sauter ces caractËres
- default: s[j++]=(char) c; break;
- }
- }
- } while((c!=-1) && (c!=EOF) && (j<(max-1)));
- s[j]='\0';
- return j;
- }
- int linput_trim(FILE* fp,char* s,int max) {
- int rlen=0;
- char* ls=(char*) malloct(max+2);
- s[0]='\0';
- if (ls) {
- char* a;
- // lire ligne
- rlen=linput(fp,ls,max);
- if (rlen) {
- // sauter espaces et tabs en fin
- while( (rlen>0) && ((ls[max(rlen-1,0)]==' ') || (ls[max(rlen-1,0)]=='\t')) )
- ls[--rlen]='\0';
- // sauter espaces en dÈbut
- a=ls;
- while((rlen>0) && ((*a==' ') || (*a=='\t'))) {
- a++;
- rlen--;
- }
- if (rlen>0) {
- memcpy(s,a,rlen); // can copy \0 chars
- s[rlen]='\0';
- }
- }
- //
- freet(ls);
- }
- return rlen;
- }
- int linput_cpp(FILE* fp,char* s,int max) {
- int rlen=0;
- s[0]='\0';
- do {
- int ret;
- if (rlen>0)
- if (s[rlen-1]=='\\')
- s[--rlen]='\0'; // couper \ final
- // lire ligne
- ret=linput_trim(fp,s+rlen,max-rlen);
- if (ret>0)
- rlen+=ret;
- } while((s[max(rlen-1,0)]=='\\') && (rlen<max));
- return rlen;
- }
-
- // idem avec les car spÈciaux
- void rawlinput(FILE* fp,char* s,int max) {
- int c;
- int j=0;
- do {
- c=fgetc(fp);
- if (c!=EOF) {
- switch(c) {
- case 13: break; // sauter CR
- case 10: c=-1; break;
- default: s[j++]=(char) c; break;
- }
- }
- } while((c!=-1) && (c!=EOF) && (j<(max-1)));
- s[j++]='\0';
- }
-
- // Like linput, but in memory (optimized)
- int binput(char* buff,char* s,int max) {
- char* end;
- int count;
-
- // clear buffer
- s[0]='\0';
- // end of buffer?
- if ( *buff == '\0')
- return 1;
- // find ending \n
- end=strchr(buff,'\n');
- // ..or end of buffer
- if (!end)
- end=buff+strlen(buff);
- // then count number of bytes, maximum=max
- count=min(max,end-buff);
- // and strip annoying ending cr
- while( (count>0) && (buff[count] == '\r'))
- count--;
- // copy
- if (count > 0) {
- strncatbuff(s, buff, count);
- }
- // and terminate with a null char
- s[count]='\0';
- // then return the supplemental jump offset
- return (end-buff)+1;
- }
-
- int fexist(char* s) {
- FILE* fp;
- fp=fopen(s,"rb");
- if (fp!=NULL) fclose(fp);
- return (fp!=NULL);
- }
-
- int fsize(char* s) {
- FILE* fp;
- fp=fopen(s,"rb");
- if (fp!=NULL) {
- int i;
- fseek(fp,0,SEEK_END);
- i=ftell(fp);
- fclose(fp);
- return i;
- } else return -1;
- }
-
- TStamp time_local(void) {
- return ((TStamp) time(NULL));
- }
-
-
-
-
-
- #define NOSTATIC_RESERVE(name, type, nelt) NOSTATIC_XRESERVE(name, type, nelt)
- #define NOSTATIC_XRESERVE(name, type, nelt) do { \
- __declspec( thread ) static type thValue[nelt]; \
- __declspec( thread ) int static initValue = 0; \
- name = thValue; \
- if (!initValue) { \
- initValue = 1; \
- memset(&thValue, 0, sizeof(thValue)); \
- } \
- } while(0)
-
-
-
- typedef struct {
- char buff[16][HTS_URLMAXSIZE*2*2];
- int rol;
- } concat_strc;
- char* concat(const char* a,const char* b) {
- concat_strc* strc;
- NOSTATIC_RESERVE(strc, concat_strc, 1);
- strc->rol=((strc->rol+1)%16); // roving pointer
- strcpybuff(strc->buff[strc->rol],a);
- if (b) strcatbuff(strc->buff[strc->rol],b);
- return strc->buff[strc->rol];
- }
- // conversion fichier / -> antislash
- #if HTS_DOSNAME
- char* __fconv(char* a) {
- int i;
- for(i=0;i<(int) strlen(a);i++)
- if (a[i]=='/') // convertir
- a[i]='\\';
- return a;
- }
- char* fconcat(char* a,char* b) {
- return __fconv(concat(a,b));
- }
- char* fconv(char* a) {
- return __fconv(concat(a,""));
- }
- #endif
-
- /* / et \\ en / */
- char* __fslash(char* a) {
- int i;
- for(i=0;i<(int) strlen(a);i++)
- if (a[i]=='\\') // convertir
- a[i]='/';
- return a;
- }
- char* fslash(char* a) {
- return __fslash(concat(a,""));
- }
-
- // conversion minuscules, avec buffer
- char* convtolower(char* a) {
- concat_strc* strc;
- NOSTATIC_RESERVE(strc, concat_strc, 1);
- strc->rol=((strc->rol+1)%16); // roving pointer
- strcpybuff(strc->buff[strc->rol],a);
- hts_lowcase(strc->buff[strc->rol]); // lower case
- return strc->buff[strc->rol];
- }
-
- // conversion en minuscules
- void hts_lowcase(char* s) {
- int i;
- for(i=0;i<(int) strlen(s);i++)
- if ((s[i]>='A') && (s[i]<='Z'))
- s[i]+=('a'-'A');
- }
-
- char* next_token(char* p,int flag) {
- int detect=0;
- int quote=0;
- p--;
- do {
- p++;
- if (flag && (*p=='\\')) { // sauter \x ou \"
- if (quote) {
- char c='\0';
- if (*(p+1)=='\\')
- c='\\';
- else if (*(p+1)=='"')
- c='"';
- if (c) {
- char tempo[8192];
- tempo[0]=c; tempo[1]='\0';
- strcatbuff(tempo,p+2);
- strcpybuff(p,tempo);
- }
- }
- }
- else if (*p==34) { // guillemets (de fin)
- quote=!quote;
- }
- else if (*p==32) {
- if (!quote)
- detect=1;
- }
- else if (*p=='\0') {
- p=NULL;
- detect=1;
- }
- } while(!detect);
- return p;
- }
-